package com.unitd.frame.springweb.helper;

import com.unitd.frame.comm.utils.CharsetUtils;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.Model;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.mail.internet.MimeMessage;
import java.util.Map;

/**
 * @desc 邮件发送工具类
 * @filename MailHelper.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/4/11
 */
public class MailHelper {
	protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	private VelocityEngine velocityEngine;
	private JavaMailSender mailSender;
	private String charset;
	private String mailTitle;
	private String mailFrom;

	public MailHelper() {
		this.charset = CharsetUtils.UTF_8.name();
	}

	public boolean sendMail(String to, String subject, String tplName, Model model) {
		return this.sendMail(to, subject, tplName, model.asMap());
	}

	public boolean sendMail(String[] to, String subject, String tplName, Model model) {
		return this.sendMail(to, subject, tplName, model.asMap());
	}

	public boolean sendMail(String to, String subject, String tplName, Map<String, Object> data) {
		return this.sendMail(new String[]{to}, subject, tplName, data);
	}

	public boolean sendMail(String[] to, String subject, String tplName, Map<String, Object> data) {
		return this.sendMail(this.mailTitle, this.mailFrom, to, subject, tplName, data);
	}

	public boolean sendMail(String personal, String from, String to, String subject, String tplName, Model model) {
		return this.sendMail(personal, from, new String[]{to}, subject, tplName, model.asMap());
	}

	public boolean sendMail(String personal, String from, String[] to, String subject, String tplName, Model model) {
		return this.sendMail(personal, from, to, subject, tplName, model.asMap());
	}

	public boolean sendMail(String personal, String from, String[] to, String subject, String tplName, Map<String, Object> data) {
		try {
			MimeMessage e = this.mailSender.createMimeMessage();
			MimeMessageHelper msgHelper = new MimeMessageHelper(e, this.getCharset());
			msgHelper.setFrom(from, personal);
			msgHelper.setTo(to);
			msgHelper.setSubject(subject);
			msgHelper.setText(this.getHtmltext(tplName, data), true);
			this.mailSender.send(e);
			return true;
		} catch (Exception var9) {
			this.logger.error("send mail error.", var9);
			return false;
		}
	}

	public String getHtmltext(String tplName, Map<String, Object> data) {
		return VelocityEngineUtils.mergeTemplateIntoString(this.velocityEngine, tplName, this.getCharset(), data);
	}

	public String getHtmltext(String tplName, Model model) {
		return this.getHtmltext(tplName, model.asMap());
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public String getCharset() {
		return this.charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getMailTitle() {
		return this.mailTitle;
	}

	public void setMailTitle(String mailTitle) {
		this.mailTitle = mailTitle;
	}

	public String getMailFrom() {
		return this.mailFrom;
	}

	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}
}